package WinterHomework.day10;

import java.util.LinkedList;
import java.util.Queue;

//Leetcode189号问题。旋转数组
public class Demo01 {
    public static void main(String[] args) {

    }

    public void rotate(int[] nums, int k) {
        if (nums == null || k <= 0) {
            return;
        }
        //需要考虑K大于数组长度的问题
        k %= nums.length;
        //自己做的
//        Queue<Integer> queue = new LinkedList<>();
//        for (int i = nums.length - k; i < nums.length; i++) {
//            queue.offer(nums[i]);
//        }
//        for (int i = 0; i < nums.length - k; i++) {
//            queue.offer(nums[i]);
//        }
//        for (int i = 0; i < nums.length; i++) {
//            nums[i]=queue.poll();
//        }
        //数组反转,先反转整体，再反转前k个，再反转剩下的
        reverse(nums,0, nums.length-1);
        reverse(nums,0,k-1);
        reverse(nums,k, nums.length-1);
    }

    public void reverse(int[] arr, int start, int end) {
        while (start < end) {
            int temp = arr[end];
            arr[end] = arr[start];
            arr[start] = temp;
            start++;
            end--;
        }
    }
}
